const productApi = require('../../api/product')

Page({
  data: {
    productList: [],
    loading: false,
    hasMore: true,
    page: 1,
    size: 10,
    keyword: '',
    searchValue: '',
    storeManagerId: null,
    refreshing: false
  },

  onLoad(options) {
    // 从参数中获取店长ID，如果没有则获取所有商品
    if (options.storeManagerId) {
      this.setData({
        storeManagerId: parseInt(options.storeManagerId)
      })
    }
    this.loadProductList()
  },

  onShow() {
    // 页面显示时刷新数据
    this.refreshData()
  },

  /**
   * 加载商品列表
   */
  async loadProductList(isRefresh = false) {
    if (this.data.loading) return

    this.setData({ loading: true })

    try {
      const params = {
        storeManagerId: this.data.storeManagerId,
        page: isRefresh ? 1 : this.data.page,
        size: this.data.size,
        keyword: this.data.keyword
      }

      const result = await productApi.getProductList(params)
      
      if (result && result.records) {
        const newList = isRefresh ? result.records : [...this.data.productList, ...result.records]
        
        this.setData({
          productList: newList,
          hasMore: result.records.length === this.data.size,
          page: isRefresh ? 2 : this.data.page + 1,
          loading: false,
          refreshing: false
        })
      } else {
        this.setData({
          loading: false,
          refreshing: false,
          hasMore: false
        })
      }
    } catch (error) {
      console.error('加载商品列表失败:', error)
      wx.showToast({
        title: error.message || '加载失败',
        icon: 'none'
      })
      this.setData({
        loading: false,
        refreshing: false
      })
    }
  },

  /**
   * 刷新数据
   */
  refreshData() {
    this.setData({
      productList: [],
      page: 1,
      hasMore: true,
      refreshing: true
    })
    this.loadProductList(true)
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh() {
    this.refreshData()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 上拉加载更多
   */
  onReachBottom() {
    if (this.data.hasMore && !this.data.loading) {
      this.loadProductList()
    }
  },

  /**
   * 搜索输入
   */
  onSearchInput(e) {
    this.setData({
      searchValue: e.detail.value
    })
  },

  /**
   * 执行搜索
   */
  onSearch() {
    this.setData({
      keyword: this.data.searchValue,
      productList: [],
      page: 1,
      hasMore: true
    })
    this.loadProductList(true)
  },

  /**
   * 清空搜索
   */
  onClearSearch() {
    this.setData({
      searchValue: '',
      keyword: '',
      productList: [],
      page: 1,
      hasMore: true
    })
    this.loadProductList(true)
  },

  /**
   * 点击商品项
   */
  onProductTap(e) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/product-detail/product-detail?id=${id}`
    })
  },

  /**
   * 格式化价格显示
   */
  formatPrice(price) {
    return parseFloat(price).toFixed(2)
  },

  /**
   * 获取商品状态文本
   */
  getStatusText(status, stock) {
    if (status !== 1) {
      return '已下架'
    }
    if (stock <= 0) {
      return '缺货'
    }
    return '有货'
  },

  /**
   * 获取会员等级样式类
   */
  getMemberLevelClass(memberLevel) {
    switch (memberLevel) {
      case 'VIP1':
        return 'vip1'
      case 'VIP2':
        return 'vip2'
      case 'VIP3':
        return 'vip3'
      default:
        return 'normal'
    }
  }
}) 